package com.egg.ylt.controls;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.egg.ylt.R;
import com.egg.ylt.Utils.API;
import com.egg.ylt.Utils.Constants;
import com.egg.ylt.adapter.ADA_NewCouponList;
import com.egg.ylt.pojo.CouponEntity;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.yonyou.framework.library.bean.ErrorBean;
import com.yonyou.framework.library.common.CommonUtils;
import com.yonyou.framework.library.common.utils.ListUtil;
import com.yonyou.framework.library.net.ReqCallBack;
import com.yonyou.framework.library.net.RequestManager;

import java.util.HashMap;

import okhttp3.Call;

/**
 * author:        yupeng
 * created on:    2019/3/1 11:08
 * description:   首页优惠券弹窗
 */
public class DIALOG_HomepageCoupon extends PopupWindow {
    private LinearLayout rootLayout;
    private LinearLayout popLayout;
    private ImageView dismissButton;
    private XRecyclerView xRecyclerView;
    private TextView tipsTxt;
    private ADA_NewCouponList adapter;
    private Call call;
    private Context context;

    public DIALOG_HomepageCoupon(Context context, CouponEntity entity) {
        super(context);
        rootLayout = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.dialog_homepage_coupon, null);
        popLayout = rootLayout.findViewById(R.id.pop_layout);
        dismissButton = rootLayout.findViewById(R.id.dismiss_button);
        xRecyclerView = rootLayout.findViewById(R.id.xRecyclerView);
        tipsTxt = rootLayout.findViewById(R.id.tips_txt);

        this.context = context;
        // 设置SelectPicPopupWindow的View
        this.setContentView(rootLayout);
        // 设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        // 设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        // 设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        this.setTouchable(true);
        this.setOutsideTouchable(true);  //设置点击屏幕其它地方弹出框消失
        // 实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0xb0000000);
        // 设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);

        // rootLayout父布局 popLayout子布局
        // rootLayout 添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
        rootLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            }
        });
        popLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            }
        });

        LinearLayoutManager layoutManager = new LinearLayoutManager(context);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        xRecyclerView.setLayoutManager(layoutManager);
        xRecyclerView.setPullRefreshEnabled(false);
        xRecyclerView.setLoadingMoreEnabled(false);
        xRecyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
                //判断是当前layoutManager是否为LinearLayoutManager
                // 只有LinearLayoutManager才有查找第一个和最后一个可见view位置的方法
                if (layoutManager instanceof LinearLayoutManager) {
                    LinearLayoutManager linearManager = (LinearLayoutManager) layoutManager;
                    //获取最后一个可见view的位置
                    int lastItemPosition = linearManager.findLastVisibleItemPosition();
                    tipsTxt.setVisibility(lastItemPosition < adapter.getDataList().size() ? View.VISIBLE : View.GONE);
                }
            }
        });

        adapter = new ADA_NewCouponList(context);
        adapter.update(entity.getList());
        xRecyclerView.setAdapter(adapter);
        tipsTxt.setVisibility(ListUtil.isListNotEmpty(adapter.getDataList()) && adapter.getDataList().size() <= 3
                ? View.GONE : View.VISIBLE);
        adapter.setOnButtonClickListener(new ADA_NewCouponList.OnButtonClickListener() {
            @Override
            public void onClick(CouponEntity.ListEntity model, int position) {
                requestObtainCoupon(model, position);
            }
        });

        dismissButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (call != null) {
                    call.cancel();
                }
                dismiss();
            }
        });
    }

    private void requestObtainCoupon(final CouponEntity.ListEntity model, final int position) {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("token", Constants.TOKEN);
        hashMap.put("memberCompanyId", model.getMemberCompanyId());
        hashMap.put("ticketId", model.getTicketId());
        hashMap.put("activityId", model.getActivityId());
        hashMap.put("id", model.getId());
        call = RequestManager.getInstance().requestPostByAsyn(API.RECEIVE_COUPON, hashMap, new ReqCallBack<Object>() {
            @Override
            public void onReqSuccess(Object result) {
                CommonUtils.makeEventToast(context, "领取成功", false);
                model.setStatus("0");
                adapter.getDataList().set(adapter.getDataList().indexOf(model), model);
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onFailure(String result) {
                CommonUtils.makeEventToast(context, "领取失败，请重新尝试", false);
            }

            @Override
            public void onReqFailed(ErrorBean error) {
                CommonUtils.makeEventToast(context, "领取失败，请重新尝试", false);
            }
        });
    }

    public void show() {
        this.showAtLocation(rootLayout, Gravity.CENTER, 0, 0);
    }
}
